<template>
  <div class="weather-map">
    <div class="arcgis map" id="WeatherArcGis"></div>
    <div class="btn-list">
      <button @click="toPlay">播放</button
      ><button @click="toPlayImage">播放Image</button>
    </div>
  </div>
</template>

<script>
import rainImage from "./base64.json";
import rainData from "./weatherRain.json";
import { loadModules } from "esri-loader";
const rainLayerId = "rainLayer";
export default {
  name: "index",
  data() {
    return {
      map: null,
      mapView: null,
      Graphic: null,
      markers: [],
      SimpleFillSymbol: null,
      MediaLayer: null,
      rainIndex: 0,
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    async initMap() {
      const [Map, MapView, Polygon, Graphic, SimpleFillSymbol, MediaLayer] =
        await loadModules(
          [
            "esri/Map",
            "esri/views/MapView",
            "esri/geometry/Polygon",
            "esri/Graphic",
            "esri/symbols/SimpleFillSymbol",
            "esri/layers/MediaLayer",
          ],
          { version: "4.24", css: true }
        );

      this.Graphic = Graphic;
      this.SimpleFillSymbol = SimpleFillSymbol;
      this.MediaLayer = MediaLayer;

      this.map = new Map({
        basemap: "streets-vector",
      });

      this.mapView = new MapView({
        container: "WeatherArcGis",
        center: [114.296173, 30.592868],
        map: this.map,
        zoom: 4,
      });
      this.setMapConstraints(Polygon);
    },
    toPlay() {
      if (this.mapView.graphics.length) {
        this.mapView.graphics.removeAll();
      }
      let target = rainData[this.rainIndex];
      for (let key in target) {
        let graphic = this.addRainGraphic(target[key]);
        this.mapView.graphics.add(graphic);
      }
      if (this.rainIndex === 0) {
        this.rainIndex = 1;
      } else {
        this.rainIndex = 0;
      }
    },
    // 添加降雨图层
    addRainGraphic(target) {
      const { rings, color } = target;
      const boundary = {
        type: "polygon",
        rings,
        spatialReference: { wkid: 4326 },
      };

      const fillSymbol = new this.SimpleFillSymbol({
        color, // 浅蓝色，50%透明度
        outline: {
          color: [0, 0, 0, 0], // 透明边框
          width: 1,
        },
      });

      const graphic = new this.Graphic({
        geometry: boundary,
        symbol: fillSymbol,
      });

      return graphic;
    },
    async toPlayImage() {
      const { img } = rainImage[50];
      const image = "data:image/png;base64," + img;
      const [ImageElement, ExtentAndRotationGeoreference, Extent] =
        await loadModules([
          "esri/layers/support/ImageElement",
          "esri/layers/support/ExtentAndRotationGeoreference",
          "esri/geometry/Extent",
        ]);
      const element = new ImageElement({
        image: image,
        georeference: new ExtentAndRotationGeoreference({
          extent: new Extent({
            xmin: 113.65,
            ymin: 24.549999,
            xmax: 118.45,
            ymax: 30.049999,
          }),
        }),
      });
      console.log(image);
      let layer = new this.MediaLayer({
        id: rainLayerId,
        title: "预报降雨",
        source: [element],
        opacity: 0.9,
        blendMode: "normal",
      });
      this.map.add(layer);
    },

    // 设置地图边界限制
    setMapConstraints(Polygon) {
      const chinaBoundary = new Polygon({
        // 中国边界 简易版的
        rings: [
          [
            [73.560833, 3.828125],
            [135.087891, 3.828125],
            [135.087891, 53.565427],
            [73.560833, 53.565427],
            [73.560833, 3.828125],
          ],
        ],
        spatialReference: { wkid: 4326 },
      });

      this.mapView.constraints = {
        geometry: chinaBoundary,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.weather-map {
  width: 100%;
  height: 100%;
  position: relative;
}
.map {
  width: 100%;
  height: 100%;
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
}

.btn-list {
  width: auto;
  height: auto;
  position: absolute;
  right: 15px;
  top: 15px;
  z-index: 5;
  display: flex;
  flex-flow: column;
  .btn {
    display: block;
  }
}
</style>
